﻿var grid = null;
$(function () {
    $("#txtDate").ligerDateEditor({
        showTime: false,
        format: "yyyy-MM-dd",
        width: 90,
        cancelable: false,
        onChangeDate: function () {
            search();
        }
    });

    var isDeptAll = 0;
    if ($("#hdnJobTitle").val().indexOf("护理部") > -1) isDeptAll = 1;
    $("#ddlDepartmentCode").ligerComboBox({
        cancelable: false,
        width: 100,
        url: "/Escort/EscortPersonnel/GetDepartmentList?isAll=" + isDeptAll + "&ram" + Math.random(),
        valueField: "value",
        textField: "text",
        valueFieldID: 'hdnDepartmentCode',
        value: $("#hdnJobTitle").val().indexOf("护理部") > -1 ? "-1" : $("#hdnCurrDepCode").val(),
        onSelected: function () {
            search();
        }
    });

    $("#ddlComeFrom").ligerComboBox({
        cancelable: false,
        width: 100,
        url: "/Escort/EscortPersonnel/GetBaseCommonDict?isAll=1&dictName=OUT_COME_DICT&ram" + Math.random(),
        valueField: "ITEM_CODE",
        textField: "ITEM_NAME",
        valueFieldID: 'hdnComeFrom',
        value: -1,
        onSelected: function () {
            search();
        }
    });

    $("#ddlESCORT_TYPE").ligerComboBox({
        cancelable: false,
        width: 60,
        data: [
              { id: 1, text: "临时" },
              { id: 2, text: "长期" }
        ],
        valueFieldID: 'hdnESCORT_TYPE',
        value: 2,
        onSelected: function () {
            search();
        }
    });

    $('#btnAdd').click(function () {
        edit(1);
    });

    $('#btnEdit').click(function () {
        edit(2);
    });

    $('#btnStop').click(function () {
        stop();
    });

    $('#btnSearch').click(function () {
        search();
    });

    $('#btnPrintEscortCard').click(function () {
        printView(1);
    });

    $('#btnPrintOutCard').click(function () {
        printView(2);
    });

    $('#btnPrintHealthClaim').click(function () {
        printView(3);
    });

    $('#btnPrintAddEscort').click(function () {
        printView(4);
    });
    
    $("#btnExport").click(function () {
        var date = $("#txtDate").val();
        var departmentCode = $("#hdnDepartmentCode").val();
        var comeFrom = $("#hdnComeFrom").val();
        var stopDate = $("#txtStopDate").val();
        var escortType = $("#hdnESCORT_TYPE").val();
        var url = "/Escort/EscortPersonnel/Export?date=" + date + "&departmentCode=" + departmentCode + "&comeFrom=" + comeFrom + "&stopDate=" + stopDate + "&escortType=" + escortType;
        Export(url);
    })

    $('#btnSearch').click();
});

function getSummaryInfo() {
    $.ajax({
        url: "/Escort/EscortPersonnel/GetSummaryInfo",
        type: "GET",
        cache: false,
        data: { departmentCode: $("#hdnDepartmentCode").val() },
        success: function (data) {
            var d = $.parseJSON(data);
            if (d.Result == 1) {
                var body = $.parseJSON(d.Body)[0];
                $("#lblALL_PATIENT").text(body.ALL_PATIENT);
                $("#lblALL_ESCORT").text(body.ALL_ESCORT);
                $("#lblALL_CITY").text(body.ALL_CITY);
                $("#lblALL_PRO").text(body.ALL_PRO);
                $("#lblALL_OTHER_PRO").text(body.ALL_OTHER_PRO);
                $("#lblALL_KEY_AREA").text(body.ALL_KEY_AREA);
                $("#lblALL_OVER").text(body.ALL_OVER);
                $("#lblALL_LESS_14").text(body.ALL_LESS_14);
            }
            else if (d.Result == 0) {
                $.ligerDialog.error(d.Body);
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        },
        error: function () {
            $.ligerDialog.error("程序错误");
        }
    });
}

//搜索
function search(pageNumber) {
    var queryData = {
        date: $("#txtDate").val(),
        departmentCode: $("#hdnDepartmentCode").val(),
        comeFrom: $("#hdnComeFrom").val(),
        stopDate: $("#txtStopDate").val(),
        escortType: $("#hdnESCORT_TYPE").val()
    }
    initTable(queryData, pageNumber);

    getSummaryInfo();
}

var KEY_AREAS_DICT = [];

//加载列表
function initTable(queryData, pageNumber) {
    grid = $("#maingrid").ligerGrid({
        height: '100%',
        url: '/Escort/EscortPersonnel/Search?ram=' + Math.random(),
        parms: queryData,
        page: pageNumber,
        pageSize: 30,
        rownumbers: true,
        heightDiff: -5,
        fixedCellHeight: false,
        columns: [
            { display: '患者床号', name: 'BED_NO', width: 80 },
            { display: '患者姓名', name: 'PATIENT_NAME', width: 80 },
            { display: '住院号', name: 'PATIENT_ID', width: 80 },
            { display: '病区', name: 'NURSE_CELL_NAME', width: 100 },
            { display: '陪护姓名', name: 'ESCORT_NAME', width: 80 },
            {
                display: '陪护开始日期', name: 'START_DATE', width: 100, render: function (row) {
                    return row.START_DATE.split(' ')[0];
                }
            },
            { display: '陪护年龄', name: 'ESCORT_AGE', width: 80 },
            {
                display: '陪护性别', name: 'ESCORT_SEX_NAME', width: 80, render: function (row) {
                    if (IsEmpty(row.ESCORT_ID)) return "";
                    else return row.ESCORT_SEX_NAME;
                }
            },
            { display: '陪护联系方式', name: 'ESCORT_PHONE', width: 140 },
            { display: '与患者亲属关系', name: 'KINSHIP_NAME', width: 120},
            { display: '陪护籍贯', name: 'ADDR_NAME', width: 130 },
            { display: '证件类型', name: 'ID_CARD_TYPE_NAME', width: 80 },
            { display: '证件号', name: 'ID_CARD_NO', width: 150 },
            {
                display: '陪护联系地址', name: 'ADDR_DETAIL', width: 250, render: function (row) {
                    return row.ADDR_DETAIL.replace(/#/g, "")
                }
            },
            {
                display: '陪护人员来源', name: 'OUT_COME_FLAG', width: 100, render: function (row) {
                    return $("#ddlComeFrom").ligerComboBox().findTextByValue(row.OUT_COME_FLAG);
                }
            },
            {
                display: '健康码', name: 'HEALTH_CODE_TYPE', width: 100, render: function (row) {
                    if (row.HEALTH_CODE_TYPE==1) {
                        return "绿码";
                    }
                    else if (row.HEALTH_CODE_TYPE == 2) {
                        return "橙码";
                    }
                    else if (row.HEALTH_CODE_TYPE == 3) {
                        return "红码";
                    }
                    else {
                        return "无";
                    }
                }
            },
            {
                display: '来泉日期', name: 'COME_FROM_DATE', width: 100, render: function (row) {
                    return row.COME_FROM_DATE.split(' ')[0];
                }
            },
            {
                display: '重点疫区接触史', name: 'KEY_AREAS_FLAG', width: 100, render: function (row) {
                    if (IsEmpty(row.ESCORT_ID)) return "";
                    if (row.KEY_AREAS_FLAG == 1)
                        return "有";
                    else
                        return "无";
                }
            },
            {
                display: '重点疫区接触地', name: 'KEY_AREAS_DESC', width: 250, render: function (row) {
                    if (IsEmpty(row.ESCORT_ID)) return "";
                    var html = "";
                    for (var i = 0; i < KEY_AREAS_DICT.length; i++) {
                        if ((';' + row.KEY_AREAS_DESC + ';').indexOf(';' + KEY_AREAS_DICT[i].value + ';') > -1) {
                            html += KEY_AREAS_DICT[i].text + ";";
                        }
                    }
                    return html;
                }
            },
            {
                display: '是否境外归国人员', name: 'ABROAD_FLAG', width: 100, render: function (row) {
                    if (IsEmpty(row.ESCORT_ID)) return "";
                    if (row.ABROAD_FLAG == 1)
                        return "是";
                    else
                        return "否";
                }
            },
            {
                display: '归国日期', name: 'ENTRY_DATE', width: 100, render: function (row) {
                    return row.ENTRY_DATE.split(' ')[0];
                }
            },
            { display: '境外国家', name: 'ABROAD_COUNTRY', width: 100 },
            {
                display: '有无流行病史', name: 'EPIDEMIC_FLAG', width: 100, render: function (row) {
                    if (IsEmpty(row.ESCORT_ID)) return "";
                    if (row.EPIDEMIC_FLAG == 1)
                        return "有";
                    else
                        return "无";
                }
            },
            {
                display: '有无临床症状', name: 'CLINIC_SYMP_FLAG', width: 100, render: function (row) {
                    if (IsEmpty(row.ESCORT_ID)) return "";
                    if (row.CLINIC_SYMP_FLAG == 1)
                        return "有";
                    else
                        return "无";
                }
            }
        ],
        onSuccess: function (data, grid) {
            if (KEY_AREAS_DICT.length == 0) {
                $.ajax({
                    url: "/Escort/EscortPersonnel/GetBaseCommonDict?dictName=KEY_AREAS_DICT",
                    type: "GET",
                    async: false,
                    success: function (data) {
                        var jsonData = $.parseJSON(data);
                        for (var i = 0; i < jsonData.length; i++) {
                            KEY_AREAS_DICT.push({ text: jsonData[i].ITEM_NAME, value: jsonData[i].ITEM_CODE });
                        }
                    }
                });
            }
        }
    });
    GridNoData($("#maingrid"));
}

//添加/编辑
function edit(type) {
    var row = grid.getSelectedRow();
    if (!row) {
        if (type == 1)
            $.ligerDialog.error("请选择患者");
        else
            $.ligerDialog.error("请选择陪护人员");
        return false;
    }
    var id = "";
    if (type == 2) {
        id = row.ESCORT_ID;
        if (IsEmpty(id)) {
            $.ligerDialog.error("请选择陪护人员");
            return;
        }
    }

    $.ligerDialog.open({
        height: 620,
        width: 550,
        title: '编辑陪护人员',
        url: '/Escort/EscortPersonnel/Edit?id=' + id,
        showMax: false,
        showToggle: false,
        showMin: false,
        isResize: false,
        slide: false,
        data: { BED_NO: row.BED_NO, PATIENT_NAME: row.PATIENT_NAME, PID: row.PATIENT_ID, VID: row.VISIT_ID, NURSE_CELL_CODE: row.NURSE_CELL_CODE }
    });
}

//停止
function stop() {
    var row = grid.getSelectedRow();
    if (!row) {
        $.ligerDialog.error("请选择陪护人员");
        return false;
    }
    $.ligerDialog.confirm('确定停止？', function (value) {
        if (value) {
            var url = "/Escort/EscortPersonnel/Stop";
            $.ajax({
                url: url,
                type: "POST",
                cache: false,
                data: { id: row.ESCORT_ID },
                success: function (data) {
                    var d = $.parseJSON(data);
                    if (d.Result == 1) {
                        $.ligerDialog.success("停止成功");
                        $('#btnSearch').click();
                    }
                    else if (d.Result == 0) {
                        $.ligerDialog.error(d.Body);
                    }
                    else {
                        $.ligerDialog.error("程序错误");
                    }
                },
                error: function () {
                    $.ligerDialog.error("程序错误");
                }
            });
        }
    });
}

//打印预览
function printView(type) {
    var printDataUrl = "";
    var printGrfUrl = "";
    var row = grid.getSelectedRow();
    if (type == 1) {
        if (!row || (IsEmpty(row.ESCORT_ID) && $("#hdnESCORT_ID").val() == "")) {
            $.ligerDialog.error("请选择陪护人员");
            return false;
        }
        var eid = $("#hdnESCORT_ID").val();
        if (IsEmpty(eid)) {
            eid = row.ESCORT_ID;
        }
        else {
            $("#hdnESCORT_ID").val('');
        }

        printGrfUrl = "/Grf/陪护证.grf";
        printDataUrl = "/Escort/EscortPersonnel/GetEscortCardPrint?eid=" + eid;
    }
    if (type == 2) {
        if (!row) {
            $.ligerDialog.error("请选择患者");
            return false;
        }
        printGrfUrl = "/Grf/出院证明.grf";
        if (IsEmpty(row.ESCORT_ID)) {
            printDataUrl = "/Escort/EscortPersonnel/GetOutCardPrint?pid=" + row.PATIENT_ID + "&vid=" + row.VISIT_ID;
        }
        else {
            printDataUrl = "/Escort/EscortPersonnel/GetOutCardPrint?eid=" + row.ESCORT_ID;
        }

    }
    if (type == 3) {
        if (!row || IsEmpty(row.ESCORT_ID)) {
            $.ligerDialog.error("请选择陪护人员");
            return false;
        }
        var eid = row.ESCORT_ID;
        printGrfUrl = "/Grf/陪护人员健康声明表.grf";
        printDataUrl = "/Escort/EscortPersonnel/GetEscortCardPrint?eid=" + eid;
    }

    if (type == 4) {
        if (!row) {
            $.ligerDialog.error("请选择患者");
            return false;
        }
        printGrfUrl = "/Grf/特殊患者增加陪护申请单.grf";
        printDataUrl = "/Escort/EscortPersonnel/GetOutCardPrint?pid=" + row.PATIENT_ID + "&vid=" + row.VISIT_ID;
    }

    var queryData = {
        printDataUrl: escape(printDataUrl),
        printGrfUrl: escape(printGrfUrl)
    };

    location.href = "/Escort/EscortPersonnel/PrintView?queryData=" + encodeURI(JSON.stringify(queryData));
}